package com.dianping.cat.consumer.problem.model.transform;

import com.dianping.cat.consumer.problem.model.IVisitor;
import com.dianping.cat.consumer.problem.model.entity.Duration;
import com.dianping.cat.consumer.problem.model.entity.Entity;
import com.dianping.cat.consumer.problem.model.entity.Entry;
import com.dianping.cat.consumer.problem.model.entity.GraphTrend;
import com.dianping.cat.consumer.problem.model.entity.JavaThread;
import com.dianping.cat.consumer.problem.model.entity.Machine;
import com.dianping.cat.consumer.problem.model.entity.ProblemReport;
import com.dianping.cat.consumer.problem.model.entity.Segment;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;


public class DefaultNativeBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private DataOutputStream m_out;

  public DefaultNativeBuilder(OutputStream out) { this(out, null); }


  public DefaultNativeBuilder(OutputStream out, IVisitor visitor) {
    this.m_out = new DataOutputStream(out);
/*  30 */     this.m_visitor = (visitor == null) ? this : visitor;
  }

  public static byte[] build(ProblemReport problemReport) {
/*  34 */     ByteArrayOutputStream out = new ByteArrayOutputStream(8192);

    build(problemReport, out);
/*  37 */     return out.toByteArray();
  }


/*  41 */   public static void build(ProblemReport problemReport, OutputStream out) { problemReport.accept(new DefaultNativeBuilder(out)); }



  public void visitDuration(Duration duration) {
/*  46 */     writeTag(1, 0);
/*  47 */     writeInt(duration.getValue());

/*  49 */     writeTag(2, 0);
/*  50 */     writeInt(duration.getCount());

/*  52 */     if (duration.getMessages() != null) {
/*  53 */       writeTag(3, 2);
/*  54 */       writeInt(duration.getMessages().size());

/*  56 */       for (String message : duration.getMessages()) {
/*  57 */         writeString(message);
      }
    }

/*  61 */     writeTag(63, 3);
  }


  public void visitEntity(Entity entity) {
/*  66 */     if (entity.getId() != null) {
/*  67 */       writeTag(1, 1);
/*  68 */       writeString(entity.getId());
    }

/*  71 */     if (entity.getType() != null) {
/*  72 */       writeTag(2, 1);
/*  73 */       writeString(entity.getType());
    }

/*  76 */     if (entity.getStatus() != null) {
/*  77 */       writeTag(3, 1);
      writeString(entity.getStatus());
    }

/*  81 */     if (!entity.getThreads().isEmpty()) {
/*  82 */       writeTag(33, 2);
/*  83 */       writeInt(entity.getThreads().size());

/*  85 */       for (JavaThread thread : entity.getThreads().values()) {
/*  86 */         thread.accept(this.m_visitor);
      }
    }

/*  90 */     if (!entity.getDurations().isEmpty()) {
/*  91 */       writeTag(34, 2);
/*  92 */       writeInt(entity.getDurations().size());

/*  94 */       for (Duration duration : entity.getDurations().values()) {
/*  95 */         duration.accept(this.m_visitor);
      }
    }

/*  99 */     if (entity.getGraphTrend() != null) {
/* 100 */       writeTag(35, 1);
/* 101 */       entity.getGraphTrend().accept(this.m_visitor);
    }

/* 104 */     writeTag(63, 3);
  }


  public void visitEntry(Entry entry) {
/* 109 */     if (entry.getType() != null) {
/* 110 */       writeTag(1, 1);
/* 111 */       writeString(entry.getType());
    }

/* 114 */     if (entry.getStatus() != null) {
/* 115 */       writeTag(2, 1);
/* 116 */       writeString(entry.getStatus());
    }

/* 119 */     if (!entry.getDurations().isEmpty()) {
/* 120 */       writeTag(33, 2);
/* 121 */       writeInt(entry.getDurations().size());

/* 123 */       for (Duration duration : entry.getDurations().values()) {
/* 124 */         duration.accept(this.m_visitor);
      }
    }

/* 128 */     if (!entry.getThreads().isEmpty()) {
/* 129 */       writeTag(34, 2);
/* 130 */       writeInt(entry.getThreads().size());

/* 132 */       for (JavaThread thread : entry.getThreads().values()) {
/* 133 */         thread.accept(this.m_visitor);
      }
    }

/* 137 */     writeTag(63, 3);
  }


  public void visitGraphTrend(GraphTrend graphTrend) {
/* 142 */     writeTag(1, 0);
/* 143 */     writeInt(graphTrend.getDuration());

/* 145 */     writeTag(2, 0);
/* 146 */     writeString(graphTrend.getFails());

/* 148 */     writeTag(63, 3);
  }


  public void visitMachine(Machine machine) {
/* 153 */     if (machine.getIp() != null) {
/* 154 */       writeTag(1, 1);
/* 155 */       writeString(machine.getIp());
    }

/* 158 */     if (!machine.getEntries().isEmpty()) {
/* 159 */       writeTag(33, 2);
/* 160 */       writeInt(machine.getEntries().size());

/* 162 */       for (Entry entry : machine.getEntries()) {
/* 163 */         entry.accept(this.m_visitor);
      }
    }

/* 167 */     if (!machine.getEntities().isEmpty()) {
/* 168 */       writeTag(34, 2);
/* 169 */       writeInt(machine.getEntities().size());

/* 171 */       for (Entity entity : machine.getEntities().values()) {
/* 172 */         entity.accept(this.m_visitor);
      }
    }

/* 176 */     writeTag(63, 3);
  }


  public void visitProblemReport(ProblemReport problemReport) {
/* 181 */     writeTag(63, 0);

/* 183 */     if (problemReport.getDomain() != null) {
/* 184 */       writeTag(1, 1);
/* 185 */       writeString(problemReport.getDomain());
    }

/* 188 */     if (problemReport.getStartTime() != null) {
/* 189 */       writeTag(2, 1);
/* 190 */       writeDate(problemReport.getStartTime());
    }

/* 193 */     if (problemReport.getEndTime() != null) {
/* 194 */       writeTag(3, 1);
/* 195 */       writeDate(problemReport.getEndTime());
    }

/* 198 */     if (problemReport.getDomainNames() != null) {
/* 199 */       writeTag(4, 2);
/* 200 */       writeInt(problemReport.getDomainNames().size());

/* 202 */       for (String domain : problemReport.getDomainNames()) {
/* 203 */         writeString(domain);
      }
    }

/* 207 */     if (problemReport.getIps() != null) {
/* 208 */       writeTag(5, 2);
/* 209 */       writeInt(problemReport.getIps().size());

/* 211 */       for (String ip : problemReport.getIps()) {
/* 212 */         writeString(ip);
      }
    }

/* 216 */     if (!problemReport.getMachines().isEmpty()) {
/* 217 */       writeTag(33, 2);
/* 218 */       writeInt(problemReport.getMachines().size());

/* 220 */       for (Machine machine : problemReport.getMachines().values()) {
/* 221 */         machine.accept(this.m_visitor);
      }
    }

/* 225 */     writeTag(63, 3);
  }


  public void visitSegment(Segment segment) {
/* 230 */     if (segment.getId() != null) {
/* 231 */       writeTag(1, 1);
/* 232 */       writeInt(segment.getId().intValue());
    }

/* 235 */     writeTag(2, 0);
/* 236 */     writeInt(segment.getCount());

/* 238 */     if (segment.getMessages() != null) {
/* 239 */       writeTag(3, 2);
/* 240 */       writeInt(segment.getMessages().size());

/* 242 */       for (String message : segment.getMessages()) {
/* 243 */         writeString(message);
      }
    }

/* 247 */     writeTag(63, 3);
  }


  public void visitThread(JavaThread thread) {
/* 252 */     if (thread.getGroupName() != null) {
/* 253 */       writeTag(1, 1);
/* 254 */       writeString(thread.getGroupName());
    }

/* 257 */     if (thread.getName() != null) {
/* 258 */       writeTag(2, 1);
/* 259 */       writeString(thread.getName());
    }

/* 262 */     if (thread.getId() != null) {
/* 263 */       writeTag(3, 1);
/* 264 */       writeString(thread.getId());
    }

/* 267 */     if (!thread.getSegments().isEmpty()) {
/* 268 */       writeTag(33, 2);
/* 269 */       writeInt(thread.getSegments().size());

/* 271 */       for (Segment segment : thread.getSegments().values()) {
/* 272 */         segment.accept(this.m_visitor);
      }
    }

/* 276 */     writeTag(63, 3);
  }

  private void writeDate(Date value) {
    try {
/* 281 */       writeVarint(value.getTime());
/* 282 */     } catch (IOException e) {
/* 283 */       throw new RuntimeException(e);
    }
  }

  private void writeInt(int value) {
    try {
/* 289 */       writeVarint(value & 0xFFFFFFFFL);
/* 290 */     } catch (IOException e) {
/* 291 */       throw new RuntimeException(e);
    }
  }

  private void writeString(String value) {
    try {
/* 297 */       this.m_out.writeUTF(value);
/* 298 */     } catch (IOException e) {
/* 299 */       throw new RuntimeException(e);
    }
  }

  private void writeTag(int field, int type) {
    try {
/* 305 */       this.m_out.writeByte((field << 2) + type);
/* 306 */     } catch (IOException e) {
/* 307 */       throw new RuntimeException(e);
    }
  }

  protected void writeVarint(long value) throws IOException {
    while (true) {
/* 313 */       if ((value & 0xFFFFFFFFFFFFFF80L) == 0L) {
/* 314 */         this.m_out.writeByte((byte)(int)value);
        return;
      }
/* 317 */       this.m_out.writeByte((byte)(int)value & 0x7F | 0x80);
/* 318 */       value >>>= 7;
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/problem/model/transform/DefaultNativeBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */